
const http = require('http');
const fs = require('fs');
const path = require('path');
const readStaticFile = require('./readStaticFile.js');

http.createServer(async (req, res, next) => {
    let urlString = req.url;
    console.log(urlString);
    // __dirname 相对路径
    // './public' 当面目录 拼接 public
    // urlString 在 public下面的所有文件
    // path.join 路径拼接

    let filePathname = path.join(__dirname, './public', urlString)
    let { data, mimetType } = await readStaticFile(filePathname, res) // 异步函数
    console.log(mimetType, '---------------------', data);

    res.writeHead(200, {
        'content-type': `${mimetType};charset=utf-8` // 响应头 类型
    });

    res.write(data); // 给前台 返数据
    res.end();
}).listen(8090);


